package com.liunian.subject.application.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.liunian.subject.application.dto.SubjectInfoDTO;
import com.liunian.subject.common.entity.PageResult;
import com.liunian.subject.common.entity.Result;
import com.liunian.subject.domain.entity.SubjectInfoBO;
import com.liunian.subject.domain.service.SubjectInfoDomainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Api(tags = "刷题controller")
@Slf4j
@RequestMapping("/subject")
public class SubjectController {
    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    @PostMapping("/add")
    @ApiOperation(value = "新增题目")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.add.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkArgument(StringUtils.isNotBlank(subjectInfoDTO.getSubjectName()), "题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "题目分数不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空");
            Preconditions.checkArgument(CollectionUtil.isNotEmpty(subjectInfoDTO.getCategoryIds()), "分类id不能为空");
            Preconditions.checkArgument(CollectionUtil.isNotEmpty(subjectInfoDTO.getLabelIds()), "标签id不能为空");
            return Result.success(subjectInfoDomainService.add(subjectInfoDTO.dtoToBo()));
        } catch (Exception e) {
            return Result.fail("新增题目失败");
        }
    }

    @PostMapping("/getSubjectPage")
    @ApiOperation(value = "分页查询题目")
    public Result<PageResult<SubjectInfoBO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPage.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "类别id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "标签id不能为空");
            PageResult<SubjectInfoBO> pageResult = subjectInfoDomainService.getSubjectPage(subjectInfoDTO.dtoToBo());
            return Result.success(pageResult);
        } catch (Exception e) {
            return Result.fail("查询题目失败");
        }
    }


    @PostMapping("/getSubjectInfo")
    @ApiOperation(value = "查询题目信息")
    public Result<SubjectInfoBO> getSubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectInfo.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkNotNull(subjectInfoDTO.getId(), "题目id不能为空");
            SubjectInfoBO subjectInfoBO = subjectInfoDomainService.querySubjectInfo(subjectInfoDTO.dtoToBo());
            return Result.success(subjectInfoBO);
        } catch (Exception e) {
            return Result.fail("查询题目信息失败");
        }
    }


}
